<?php
/*报修报事*/


namespace App\Http\Controllers\Property;

use App\Common\json;
use App\Http\Controllers\Controller;
use App\Libraries\upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Common\push;

class RepairController extends  Controller{


    private $app_key = '72103a9a13805b96a0ed55b7';        //待发送的应用程序(appKey)，只能填一个。
    private $master_secret ='590ea7bc6b3d916cab6d688a';    //主密码
    private $url = "https://api.jpush.cn/v3/push";      //推送的地址

    private $appios_key = '16f9437a241bd6bf2469b87d';        //待发送的应用程序(appKey)，只能填一个。
    private $masterios_secret ='41c2ef1cfc66d759c8fdee9c';    //主密码
    private $urlios = "https://api.jpush.cn/v3/push";      //推送的地址
    //获取自己的报修信息
    public function SelectRepair(Request $request,$type){
        if($request ->isMethod('post')){

            $user_id     = $request ->input('user_id');
            if($type  ==4) {
                $data = DB::table('repair')
                    ->where('user_repair', $user_id)
                    ->where('status',3)
                    ->orderby('repair_id','ase')
                    ->get();
                foreach ($data as $value){

                    if(empty($value ->repair_image)){
                        $repair_image  = "";
                    }else{
                        $repair_image  = explode(',',$value->repair_image);
                    }

                    $dd[]       = array(
                        'repair_id'   =>$value ->repair_id,
                        'user_repair' =>$value ->user_repair,
                        'repair_image' =>$repair_image,
                        'depict'       =>$value ->depict,
                        'status'       =>$value ->status,
                        'creation_time' =>$value ->repair_create_time,
                        'update_time'    =>$value ->repair_update_time
                    );

                }
                if ($data == true) {
                    return json::json('success', '获取用户报修数据成功', $dd);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', []);
                }
            }else{
                $data = DB::table('repair')
                    ->where('user_repair', $user_id)
                    ->where('status',$type)
                    ->orderby('repair_id','ase')
                    ->get();
                foreach ($data as $value){
                    if(empty($value ->repair_image = "")){
                        $repair_image  = "";
                    }else{
                        $repair_image  = explode(',',$value->repair_image);
                    }

                    $dd[]       = array(
                        'repair_id'   =>$value ->repair_id,
                        'user_repair' =>$value ->user_repair,
                        'repair_image' =>$repair_image,
                        'depict'       =>$value ->depict,
                        'status'       =>$value ->status,
                        'creation_time' =>$value ->repair_create_time,
                        'update_time'    =>$value ->repair_update_time
                    );
                }
                if ($data == true) {
                    return json::json('success', '获取用户报修数据成功', $dd);
                } else {
                    return json::json('failure', '由于网络或者不正当原因失败', []);
                }
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }
    //生成报修报事信息
    public function AddRepair(Request $request){
        if($request ->isMethod('post')){
            $arr         = (object)array();
            $user_id     = $request ->input('user_id');
           // $village_id  = $request ->input('village_id');
            $time          = date('Y-m-d H:i:s');
            $repair_type  = $request->input('repair_type');
            $repair_image = $request->input('repair_image');
            $depict       = $request ->input('depict');
            $data         = DB::table('repair')
                          ->insert([
                             // 'village_repair' =>$village_id,
                              'user_repair' =>$user_id,
                              'repair_type'=>$repair_type,
                              'repair_image'=>$repair_image,
                              'depict'=>$depict,
                              'repair_create_time' =>$time
                          ]);
            if($data == true){
                $select   = DB::table('user')
                          ->leftjoin('village','village.village_id','=','user.user_village')
                          ->leftjoin('work','village.village_id','=','work.work_village')
                          ->where('user_id',$user_id)
                          ->first();

                $iospush  = new push($this ->appios_key,$this ->masterios_secret,$this->urlios);
                $push   = new push($this ->app_key,$this ->master_secret,$this->url);
                if($repair_type==1){
                    $iospush    ->send_pub($select ->work_village,'本物业有一条个人报事报修信息请注意查收','6','1');
                    $push    ->send_pub($select ->work_village,'本物业有一条个人报事报修信息请注意查收','6','1');
                }else{
                    $iospush    ->send_pub($select ->work_village,'本物业有一条公共报事报修信息请注意查收','6','2');
                    $push    ->send_pub($select ->work_village,'本物业有一条公共报事报修信息请注意查收','6','2');
                }


/*                $push    ->send_pub($select ->work_phone,'有请求加你为好友','1');
                $content   ="您有一条报修信息";
                json::push($content);
                $data  = array('content'=>$content,'type'=>1);*/
                return json::json('success', '报修成功', $data);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',$arr);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //修改报修改信息
   /* public function UpdateRepair(Request $request){
        if($request ->isMethod('post')){
            $user_id     = $request ->input('repair_id');
            $repair_type  = $request->input('repair_type');
            $repair_image = $request->input('repair_image');
            $depict       = $request ->input('depict');
            $data         = DB::table('repair')
                          ->where('repair_id',$user_id)
                          ->update([
                              'repair_type' => $repair_type,
                              'repair_image' => $repair_image,
                              'depict'       =>  $depict
                          ]);
            if($data == true){
                return json::json('success', '修改成功', '');
            }else{
                return json::json('failure','由于网络或者不正当原因注册失败','');
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }*/

    //删除报修单
    public function DelRepair(Request $request){
        if($request ->isMethod('post')){
            $arr         = (object)array();
            $user_id     = $request ->input('repair_id');
            $data         = DB::table('repair')
                          ->where('repair_id',$user_id)
                          ->delete();
            if($data == true){
                return json::json('success', '删除成功', $arr);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',$arr);
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    public function CommentRepair(Request $request){
        if($request ->isMethod('post')){
            $arr         = (object)array();
            $repair_id      = $request ->input('repair_id');
            $user_id        = $request ->input('user_id');
            $quality        = $request ->input('quality');
            $speed          = $request ->input('speed');
            $content        = $request ->input('content');
            $data           = DB::table('repair_type')
                            ->insert([
                                'repairtype_repair'  => $repair_id,
                                'user_repairtype'    =>$user_id,
                                'quality'             =>$quality,
                                'speed'               =>$speed,
                                'content'             => $content,
                            ]);
            if($data == true){
                DB::table('repair')->where('repair_id',$repair_id)->update(['status'=>3]);
                return json::json('success', '评论成功', $arr);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',$arr);
            }
        }
    }


}